#ifndef SERVER_H
#define SERVER_H

#include <boost/asio.hpp>
#include <string>
#include <vector>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include "Connection.h"
#include "RequestHandler.h"

namespace tsc
{
namespace common
{

//! The top-level class of the Server.
class Server : private boost::noncopyable
{
public:
  //! Construct the server to listen on the specified TCP address and port
  explicit Server(const std::string&       address,
                  const std::string&       port,
                  boost::asio::io_service& service);



private:
  //! Initiate an asynchronous accept operation.
  void startAccept();

  //! Handle completion of an asynchronous accept operation.
  void handleAccept(const boost::system::error_code& error,
                    Connection* connection);

  //! Handle a request to stop the server.
  void handleStop();

  //! Service object used to perform asynchronous operations.
  boost::asio::io_service& mService;

  //! The signal_set is used to register for process termination notifications.
  boost::asio::signal_set mSignals;

  //! Acceptor used to listen for incoming connections.
  boost::asio::ip::tcp::acceptor mAcceptor;

  //! This Server Endpoint
  boost::asio::ip::tcp::endpoint mEndpoint;

  //! The next connection to be accepted.
  std::vector<ConnectionP_t> mConnections;

  //! The handler for all incoming requests.
  RequestHandler mRequestHandler;

  //! Server description
  std::string mDescribeStr;

  //! Server instance ID
  unsigned int mInstance;
  //! Server instance counter
  static unsigned int sInstanceCounter;
};

} // namespace common
} // namespace tsc

#endif // SERVER_H
